package com.tiantian.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.cache.RegionalInformationCache;
import com.tiantian.cache.ValueaddedservicestypeCache;
import com.tiantian.dao.AgepropertyRepositroy;
import com.tiantian.dao.AreaPropertyRepositroy;
import com.tiantian.dao.BuyersFeeRepository;
import com.tiantian.dao.CustomRepeatRepository;
import com.tiantian.dao.DrillingNumberRatioRepository;
import com.tiantian.dao.EmptypacketserviceRepository;
import com.tiantian.dao.ExtendedBuyersPurchaseCycleRepositroy;
import com.tiantian.dao.FeaturesMessageRepositroy;
import com.tiantian.dao.GenderPropertiesRepositroy;
import com.tiantian.dao.KeywordsDesignatedPraiseRepository;
import com.tiantian.dao.MatchViewsRepositroy;
import com.tiantian.dao.NextDayTaskRepository;
import com.tiantian.dao.PhotoPraiseRepository;
import com.tiantian.dao.SpecifiesReviewsRepository;
import com.tiantian.dao.TaskSelectValueAddedServicesRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Ageproperty;
import com.tiantian.domain.Areaproperty;
import com.tiantian.domain.Buyersfee;
import com.tiantian.domain.Customrepeat;
import com.tiantian.domain.Drillingnumberratio;
import com.tiantian.domain.Emptypacketservice;
import com.tiantian.domain.Featuresmessage;
import com.tiantian.domain.Genderproperties;
import com.tiantian.domain.Keywordsdesignatedpraise;
import com.tiantian.domain.Matchviews;
import com.tiantian.domain.Nextdaytask;
import com.tiantian.domain.Photopraise;
import com.tiantian.domain.Specifiesreviews;
import com.tiantian.domain.Taskselectvalueaddedservices;
import com.tiantian.domain.TaskselectvalueaddedservicesId;
import com.tiantian.domain.Valueaddedservicestype;
import com.tiantian.service.TaskselectvalueaddedservicesService;

@Service
public class TaskselectvalueaddedservicesServiceImpl extends GenericServiceImpl<Taskselectvalueaddedservices, String> implements
		TaskselectvalueaddedservicesService {

	private TaskSelectValueAddedServicesRepository repository;
	@Autowired
	private ValueaddedservicestypeCache valueaddedservicestypeCache;
	@Autowired
	private EmptypacketserviceRepository emptypacketserviceRepository;
	@Autowired
	private FeaturesMessageRepositroy featuresMessageRepositroy;
	@Autowired
	private ExtendedBuyersPurchaseCycleRepositroy extendedBuyersPurchaseCycleRepositroy;
	@Autowired
	private NextDayTaskRepository nextDayTaskRepository;
	@Autowired
	private AgepropertyRepositroy agepropertyRepositroy;
	@Autowired
	private GenderPropertiesRepositroy genderPropertiesRepositroy;
	@Autowired
	private AreaPropertyRepositroy areaPropertyRepositroy;
	@Autowired
	private RegionalInformationCache regionalInformationCache;
	@Autowired
	private MatchViewsRepositroy matchViewsRepositroy;
	@Autowired
	private CustomRepeatRepository customRepeatRepository;
	@Autowired
	private DrillingNumberRatioRepository drillingNumberRatioRepository;
	@Autowired
	private KeywordsDesignatedPraiseRepository keywordsDesignatedPraiseRepository;
	@Autowired
	private SpecifiesReviewsRepository specifiesReviewsRepository;
	@Autowired
	private PhotoPraiseRepository photoPraiseRepository;
	@Autowired
	private BuyersFeeRepository buyersFeeRepository;
	private ThreadLocal<ServiceInfo> threadLocal = new ThreadLocal<ServiceInfo>();

	@Autowired
	public void setRepository(TaskSelectValueAddedServicesRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取增值服务
	 * 
	 * @param tid
	 * @return
	 */
	public List<ServiceInfo> getByTaskId(String tid) {
		String sql = "select * from taskselectvalueaddedservices t where t.TaskID=:taskId order by t.VAServicesTypeID";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", tid);
		List<Taskselectvalueaddedservices> services = repository.queryListBySql(sql, params, Taskselectvalueaddedservices.class);
		Set<ServiceInfo> serviceInfoSet = new HashSet<ServiceInfo>();
		for (Taskselectvalueaddedservices service : services) {
			TaskselectvalueaddedservicesId serviceID = service.getId();
			serviceInfoSet.addAll(getServiceInfo(tid, serviceID.getVaservicesTypeId()));
		}

		// 放入list中
		List<ServiceInfo> serviceInfoList = new ArrayList<ServiceInfo>();
		for (ServiceInfo serviceInfo : serviceInfoSet) {
			serviceInfoList.add(serviceInfo);
		}

		// 排序
		Collections.sort(serviceInfoList, new Comparator<ServiceInfo>() {

			@Override
			public int compare(ServiceInfo o1, ServiceInfo o2) {
				if (o1 == o2) {
					return 0;
				}
				if (o1 == null) {
					return -1;
				}
				if (o2 == null) {
					return 1;
				}
				return o1.getKey().compareTo(o2.getKey());
			}
		});
		threadLocal.remove();
		return serviceInfoList;
	}

	/**
	 * 1、安全服务 1.1、空包服务 1.2、特色留言 1.3、延长买家购买周期2月 1.4、延长买家购买周期3月 1.5、隔天任务
	 * 1.5.1、2天任务 1.5.2、5天任务 2、权重优化服务 2.1、买家属性 2.1.1、性别 2.1.2、年龄
	 * 2.1.3、仅限开通花呗的买号来接任务 2.1.4、地域 2.2、浏览访客属性与勾选买家属性一致 2.2.1、匹配浏览数
	 * 2.2.2、收藏、加购物车 2.2.3、商品评价 2.3、定制回头客15天 2.4、钻号配比 2.5、指定好评 2.5.1、指定关键词好评
	 * 2.5.2、指定评语 2.5.3、指定图文好评 2.6、效率服务 2.6.1、任务插队 2.6.2、买手抢单小费1 2.6.3、买手抢单消费2
	 * 2.6.4、买手抢单消费3
	 * 
	 * @param taskId
	 * @param serviceTypeId
	 * @return
	 */
	private List<ServiceInfo> getServiceInfo(String taskId, String serviceTypeId) {
		List<ServiceInfo> ret = new ArrayList<ServiceInfo>();
		if (StringUtils.isNotBlank(serviceTypeId)) {

			if (serviceTypeId.startsWith("1")) {
				// 1.安全服务
				Valueaddedservicestype type1 = valueaddedservicestypeCache.get("1");
				ret.add(new ServiceInfo(type1.getVaservicesTypeId(), type1.getVaservicesTypeName(), null, true, 1));
				if (serviceTypeId.startsWith("11")) {
					// 1.1、空包服务
					Valueaddedservicestype type = valueaddedservicestypeCache.get(serviceTypeId);

					Emptypacketservice emptypacketservice = getEmptypacketservice(taskId);
					if (emptypacketservice != null) {
						String value = "（快递：" + emptypacketservice.getId().getCourierCompanyName() + "，重量："
								+ emptypacketservice.getId().getPackageWeight().doubleValue() + "公斤）";
						ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, type.getVaservicesTypeName(), value, false, 2);
						ret.add(serviceInfo);
					}
				} else if (serviceTypeId.startsWith("12")) {
					// 1.2特色留言
					Valueaddedservicestype type = valueaddedservicestypeCache.get(serviceTypeId);

					List<Featuresmessage> datas = getFeaturesmessage(taskId);
					if (datas != null) {
						for (Featuresmessage data : datas) {
							String value = "：" + data.getId().getLeaveMessage();
							ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, type.getVaservicesTypeName(), value, false, 2);
							ret.add(serviceInfo);
						}
					}
				} else if (serviceTypeId.startsWith("13")) {
					// 13、14延长买家购买周期
					Valueaddedservicestype type = valueaddedservicestypeCache.get("13");
					ServiceInfo serviceInfo = new ServiceInfo(type.getVaservicesTypeId(), type.getVaservicesTypeName(), "", true, 2);
					ret.add(serviceInfo);

					Valueaddedservicestype typeChild = valueaddedservicestypeCache.get(serviceTypeId);
					ServiceInfo serviceInfoChild = new ServiceInfo(serviceTypeId, typeChild.getVaservicesTypeName(), "", false, 3);
					ret.add(serviceInfoChild);

				} else if (serviceTypeId.startsWith("14")) {
					// 141,142、隔天任务
					Valueaddedservicestype type = valueaddedservicestypeCache.get("14");
					ServiceInfo info = new ServiceInfo(type.getVaservicesTypeId(), type.getVaservicesTypeName(), "", true, 2);
					ret.add(info);

					List<Nextdaytask> datas = getNextDayTask(taskId, serviceTypeId);
					if (datas != null) {
						for (Nextdaytask data : datas) {
							Valueaddedservicestype childType = valueaddedservicestypeCache.get(serviceTypeId);
							String value = "(订单数：" + data.getId().getOrderNum() + ")";
							ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, childType.getVaservicesTypeName(), value, false, 3);
							ret.add(serviceInfo);
						}
					}
				}
			} else if (serviceTypeId.startsWith("2")) {
				Valueaddedservicestype type2 = valueaddedservicestypeCache.get("2");
				ret.add(new ServiceInfo(type2.getVaservicesTypeId(), type2.getVaservicesTypeName(), null, true, 1));
				if (serviceTypeId.startsWith("21")) {
					Valueaddedservicestype type21 = valueaddedservicestypeCache.get("21");
					ServiceInfo serviceInfo21 = new ServiceInfo(type21.getVaservicesTypeId(), type21.getVaservicesTypeName(), "", true, 2);
					ret.add(serviceInfo21);

					if (serviceTypeId.startsWith("211")) {
						// 性别
						List<Genderproperties> datas = getGenderproperties(taskId);
						if (datas != null) {
							for (Genderproperties data : datas) {
								Valueaddedservicestype childType = valueaddedservicestypeCache.get(serviceTypeId);
								String value = "：";
								if (data.getId().getSex() == '0') {
									value += "女";
								} else if (data.getId().getSex() == '1') {
									value += "男";
								}
								ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, childType.getVaservicesTypeName(), value, false, 3);
								ret.add(serviceInfo);
							}
						}
					} else if (serviceTypeId.startsWith("212")) {
						// 年龄
						List<Ageproperty> datas = getAgeproperty(taskId);
						if (datas != null) {
							for (Ageproperty data : datas) {
								Valueaddedservicestype childType = valueaddedservicestypeCache.get(serviceTypeId);
								String value = "：";
								if (data.getId().getAgeNo() == 0) {
									value += "18岁以下";
								} else if (data.getId().getAgeNo() == 1) {
									value += "18-25岁";
								} else if (data.getId().getAgeNo() == 2) {
									value += "26-35岁";
								} else if (data.getId().getAgeNo() == 3) {
									value += "36岁以上";
								}
								ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, childType.getVaservicesTypeName(), value, false, 3);
								ret.add(serviceInfo);
							}
						}
					} else if (serviceTypeId.startsWith("213")) {
						Valueaddedservicestype type213 = valueaddedservicestypeCache.get("213");
						ServiceInfo serviceInfo213 = new ServiceInfo(type213.getVaservicesTypeId(), type213.getVaservicesTypeName(), "", false, 3);
						ret.add(serviceInfo213);
					} else if (serviceTypeId.startsWith("214")) {
						// 地域
						List<Areaproperty> datas = getAreaproperty(taskId);
						if (datas != null) {
							String value = null;
							Valueaddedservicestype childType = valueaddedservicestypeCache.get(serviceTypeId);
							for (Areaproperty data : datas) {
								if (value == null) {
									value = ":" + regionalInformationCache.getAreaName(data.getId().getAreaCode());
								} else {
									value += "," + regionalInformationCache.getAreaName(data.getId().getAreaCode());
								}
							}
							ServiceInfo serviceInfo = new ServiceInfo(serviceTypeId, childType.getVaservicesTypeName(), value, false, 3);
							ret.add(serviceInfo);
						}
					}
				} else if (serviceTypeId.startsWith("22")) {
					// 浏览访客属性与勾选买家属性一致
					Valueaddedservicestype type22 = valueaddedservicestypeCache.get("22");
					ServiceInfo cache22 = threadLocal.get();
					if (cache22 == null) {
						ServiceInfo serviceInfo22 = new ServiceInfo(type22.getVaservicesTypeId(), type22.getVaservicesTypeName(), "", true, 2);
						cache22 = serviceInfo22;
						threadLocal.set(cache22);
						ret.add(serviceInfo22);
						List<Matchviews> datas = getMatchViews(taskId);
						Map<String, Object> attrs = new HashMap<>();
						attrs.put("list", datas);
						serviceInfo22.attrs = attrs;
					}
					if (serviceTypeId.startsWith("221")) {
						cache22.attrs.put("key221", 1);
					} else if (serviceTypeId.startsWith("222")) {
						cache22.attrs.put("key222", 1);
					} else if (serviceTypeId.startsWith("223")) {
						cache22.attrs.put("key223", 1);
					}
				} else if (serviceTypeId.startsWith("23")) {
					// 定制回头客
					List<Customrepeat> datas = getCustomRepeat(taskId);
					if (datas != null) {
						for (Customrepeat data : datas) {
							Valueaddedservicestype type223 = valueaddedservicestypeCache.get("23");
							String value = "(回头客比例：" + data.getId().getRepeatRate() + "%,保证最少单数:" + data.getId().getMinimumOrders() + ")";
							ServiceInfo serviceInfo223 = new ServiceInfo(type223.getVaservicesTypeId(), type223.getVaservicesTypeName(), value,
									false, 2);
							ret.add(serviceInfo223);
						}
					}
				} else if (serviceTypeId.startsWith("24")) {
					// 砖石配比
					List<Drillingnumberratio> datas = getDrillingnumberratio(taskId);
					if (datas != null) {
						for (Drillingnumberratio data : datas) {
							Valueaddedservicestype type223 = valueaddedservicestypeCache.get("24");
							String value = "(钻号比率：" + data.getId().getDrillNoRatio() + "%,保证最少单数:" + data.getId().getMinimumOrders() + ")";
							ServiceInfo serviceInfo223 = new ServiceInfo(type223.getVaservicesTypeId(), type223.getVaservicesTypeName(), value,
									false, 2);
							ret.add(serviceInfo223);
						}
					}
				} else if (serviceTypeId.startsWith("25")) {
					Valueaddedservicestype type25 = valueaddedservicestypeCache.get("25");
					ServiceInfo serviceInfo25 = new ServiceInfo(type25.getVaservicesTypeId(), type25.getVaservicesTypeName(), "", true, 2);
					ret.add(serviceInfo25);
					if (serviceTypeId.startsWith("251")) {
						// 指定关键词好评
						List<Keywordsdesignatedpraise> datas = getKeywordsdesignatedpraise(taskId);
						if (datas != null) {
							for (Keywordsdesignatedpraise data : datas) {
								Valueaddedservicestype type251 = valueaddedservicestypeCache.get(serviceTypeId);
								String value = null;
								if (StringUtils.isNotBlank(data.getId().getKeyword1())) {
									if (value == null) {
										value = ":" + data.getId().getKeyword1();
									}
								}
								if (StringUtils.isNotBlank(data.getId().getKeyword2())) {
									if (value == null) {
										value = ":" + data.getId().getKeyword2();
									} else {
										value += "," + data.getId().getKeyword2();
									}
								}
								if (StringUtils.isNotBlank(data.getId().getKeyword3())) {
									if (value == null) {
										value = ":" + data.getId().getKeyword3();
									} else {
										value += "," + data.getId().getKeyword3();
									}
								}
								ServiceInfo serviceInfo251 = new ServiceInfo(type251.getVaservicesTypeId(), "指定关键词好评", value, false, 3);
								ret.add(serviceInfo251);
							}
						}
					} else if (serviceTypeId.startsWith("252")) {
						// 指定评语
						List<Specifiesreviews> datas = getSpecifiesreviews(taskId);
						if (datas != null) {
							for (Specifiesreviews data : datas) {
								Valueaddedservicestype type251 = valueaddedservicestypeCache.get(serviceTypeId);
								String value = ":" + data.getId().getReviews();
								ServiceInfo serviceInfo251 = new ServiceInfo(type251.getVaservicesTypeId(), type251.getVaservicesTypeName(), value,
										false, 3);
								ret.add(serviceInfo251);
							}
						}
					} else if (serviceTypeId.startsWith("253")) {
						// 指定图文好评
						List<Photopraise> datas = getPhotopraise(taskId);
						if (datas != null) {
							for (Photopraise data : datas) {
								Valueaddedservicestype type253 = valueaddedservicestypeCache.get(serviceTypeId);
								String value = ":" + data.getId().getPraise();
								if (StringUtils.isNotBlank(data.getId().getChasePraise())) {
									value = value.concat(",追评：" + data.getId().getChasePraise());
								}
								ServiceInfo serviceInfo253 = new ServiceInfo(type253.getVaservicesTypeId(), type253.getVaservicesTypeName(), value,
										false, 3);
								ret.add(serviceInfo253);
							}
						}
					}
				} else if (serviceTypeId.startsWith("26")) {
					Valueaddedservicestype type26 = valueaddedservicestypeCache.get("26");
					ServiceInfo serviceInfo26 = new ServiceInfo(type26.getVaservicesTypeId(), type26.getVaservicesTypeName(), "", true, 1);
					ret.add(serviceInfo26);
					if (serviceTypeId.startsWith("261")) {
						// 任务插队
						Valueaddedservicestype type253 = valueaddedservicestypeCache.get(serviceTypeId);
						ServiceInfo serviceInfo253 = new ServiceInfo(type253.getVaservicesTypeId(), type253.getVaservicesTypeName(), "", false, 2);
						ret.add(serviceInfo253);
					} else if (serviceTypeId.startsWith("262")) {

						List<Buyersfee> datas = getBuyersfee(taskId);
						if (datas != null && datas.size() > 0) {
							// 买手抢单小费
							Valueaddedservicestype type262 = valueaddedservicestypeCache.get("262");
							ServiceInfo serviceInfo262 = new ServiceInfo(type262.getVaservicesTypeId(), type262.getVaservicesTypeName(), "", true, 2);
							ret.add(serviceInfo262);

							if (datas.size() > 0) {
								Valueaddedservicestype type2621 = valueaddedservicestypeCache.get("2621");
								String value = ":" + datas.get(0).getId().getFeeAmount();
								ServiceInfo serviceInfo2621 = new ServiceInfo(type2621.getVaservicesTypeId(), type2621.getVaservicesTypeName(),
										value, true, 3);
								ret.add(serviceInfo2621);
							}
							if (datas.size() > 1) {
								Valueaddedservicestype type2622 = valueaddedservicestypeCache.get("2622");
								String value = ":" + datas.get(1).getId().getFeeAmount();
								ServiceInfo serviceInfo2622 = new ServiceInfo(type2622.getVaservicesTypeId(), type2622.getVaservicesTypeName(),
										value, true, 3);
								ret.add(serviceInfo2622);
							}
							if (datas.size() > 2) {
								Valueaddedservicestype type2623 = valueaddedservicestypeCache.get("2623");
								String value = ":" + datas.get(2).getId().getFeeAmount();
								ServiceInfo serviceInfo2623 = new ServiceInfo(type2623.getVaservicesTypeId(), type2623.getVaservicesTypeName(),
										value, true, 3);
								ret.add(serviceInfo2623);
							}
						}
					}

				}
			}
		}
		return ret;
	}

	public Map<String, List<AddServicePrice>> getPriceByTaskId(String tid) {
		String sql = "select * from taskselectvalueaddedservices t where t.TaskID=:taskId order by t.VAServicesTypeID";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", tid);
		List<Taskselectvalueaddedservices> services = repository.queryListBySql(sql, params, Taskselectvalueaddedservices.class);
		Map<String, Map<String, AddServicePrice>> serviceMap = new HashMap<String, Map<String, AddServicePrice>>();
		for (Taskselectvalueaddedservices service : services) {
			String typeId = service.getId().getVaservicesTypeId();
			Valueaddedservicestype parent = null;
			Valueaddedservicestype current = null;
			if (typeId.startsWith("26")) {
				parent = valueaddedservicestypeCache.get("26");
				current = valueaddedservicestypeCache.get(typeId);
			} else {
				parent = valueaddedservicestypeCache.get(typeId.substring(0, 1));
				current = valueaddedservicestypeCache.get(typeId);
			}

			Map<String, AddServicePrice> servicePriceMap = serviceMap.get(parent.getVaservicesTypeId());
			if (servicePriceMap == null) {
				servicePriceMap = new HashMap<String, AddServicePrice>();
				serviceMap.put(parent.getVaservicesTypeId(), servicePriceMap);
			}
			AddServicePrice servicePrice = servicePriceMap.get(typeId);
			if (servicePrice == null) {
				servicePrice = new AddServicePrice(typeId, 0, current.getVaservicesTypeName(), service.getId().getBusinessesPayBonuses());
				servicePriceMap.put(typeId, servicePrice);
			}
			servicePrice.setCount(servicePrice.getCount() + service.getId().getOrderNums());

		}

		Map<String, List<AddServicePrice>> ret = new HashMap<String, List<AddServicePrice>>();
		for (Map.Entry<String, Map<String, AddServicePrice>> entry : serviceMap.entrySet()) {
			List<AddServicePrice> values = new ArrayList<AddServicePrice>();
			values.addAll(entry.getValue().values());
			// 排序
			Collections.sort(values, new Comparator<AddServicePrice>() {
				@Override
				public int compare(AddServicePrice o1, AddServicePrice o2) {
					if (o1 == o2) {
						return 0;
					}
					if (o1 == null) {
						return -1;
					}
					if (o2 == null) {
						return 1;
					}
					return o1.getTypeId().compareTo(o2.getTypeId());
				}
			});
			ret.put(entry.getKey(), values);
		}
		return ret;
	}

	private List<Buyersfee> getBuyersfee(String taskId) {
		String sql = "select * from Buyersfee t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Buyersfee> datas = buyersFeeRepository.queryListBySql(sql, params, Buyersfee.class);
		return datas;
	}

	private List<Photopraise> getPhotopraise(String taskId) {
		String sql = "select * from Photopraise t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Photopraise> datas = photoPraiseRepository.queryListBySql(sql, params, Photopraise.class);
		return datas;
	}

	private List<Specifiesreviews> getSpecifiesreviews(String taskId) {
		String sql = "select * from Specifiesreviews t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Specifiesreviews> datas = specifiesReviewsRepository.queryListBySql(sql, params, Specifiesreviews.class);
		return datas;
	}

	private List<Keywordsdesignatedpraise> getKeywordsdesignatedpraise(String taskId) {
		String sql = "select * from Keywordsdesignatedpraise t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Keywordsdesignatedpraise> datas = keywordsDesignatedPraiseRepository.queryListBySql(sql, params, Keywordsdesignatedpraise.class);
		return datas;
	}

	private List<Drillingnumberratio> getDrillingnumberratio(String taskId) {
		String sql = "select * from drillingNumberRatio t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Drillingnumberratio> datas = drillingNumberRatioRepository.queryListBySql(sql, params, Drillingnumberratio.class);
		return datas;
	}

	private List<Customrepeat> getCustomRepeat(String taskId) {
		String sql = "select * from Customrepeat t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Customrepeat> datas = customRepeatRepository.queryListBySql(sql, params, Customrepeat.class);
		return datas;
	}

	private List<Matchviews> getMatchViews(String taskId) {
		String sql = "select * from MatchViews t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Matchviews> datas = matchViewsRepositroy.queryListBySql(sql, params, Matchviews.class);
		return datas;
	}

	private List<Areaproperty> getAreaproperty(String taskId) {
		String sql = "select * from Areaproperty t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Areaproperty> datas = areaPropertyRepositroy.queryListBySql(sql, params, Areaproperty.class);
		return datas;
	}

	private List<Genderproperties> getGenderproperties(String taskId) {
		String sql = "select * from Genderproperties t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Genderproperties> datas = genderPropertiesRepositroy.queryListBySql(sql, params, Genderproperties.class);
		return datas;
	}

	private List<Ageproperty> getAgeproperty(String taskId) {
		String sql = "select * from ageproperty t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Ageproperty> datas = agepropertyRepositroy.queryListBySql(sql, params, Ageproperty.class);
		return datas;
	}

	private List<Nextdaytask> getNextDayTask(String taskId, String serviceTypeId) {
		String sql = null;
		if ("141".equals(serviceTypeId)) {
			sql = "select * from NextDayTask t where t.TaskID=:taskId and t.NextDayNum=2";
		} else {
			sql = "select * from NextDayTask t where t.TaskID=:taskId and t.NextDayNum=5";
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Nextdaytask> datas = nextDayTaskRepository.queryListBySql(sql, params, Nextdaytask.class);
		return datas;
	}

	private List<Featuresmessage> getFeaturesmessage(String taskId) {
		String sql = "select * from FeaturesMessage t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Featuresmessage> datas = featuresMessageRepositroy.queryListBySql(sql, params, Featuresmessage.class);
		return datas;
	}

	private Emptypacketservice getEmptypacketservice(String taskId) {
		String sql = "select * from EmptyPacketService t where t.TaskID=:taskId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("taskId", taskId);
		List<Emptypacketservice> datas = emptypacketserviceRepository.queryListBySql(sql, params, Emptypacketservice.class);
		if (datas != null && datas.size() > 0) {
			return datas.get(0);
		}
		return null;
	}

	public static class AddServicePrice {
		private String typeId;
		private int count;
		private String current;
		private BigDecimal price;

		public AddServicePrice(String typeId, int count, String current, BigDecimal price) {
			super();
			this.typeId = typeId;
			this.count = count;
			this.current = current;
			this.price = price;
		}

		public String getTypeId() {
			return typeId;
		}

		public void setTypeId(String typeId) {
			this.typeId = typeId;
		}

		public int getCount() {
			return count;
		}

		public void setCount(int count) {
			this.count = count;
		}

		public String getCurrent() {
			return current;
		}

		public void setCurrent(String current) {
			this.current = current;
		}

		public BigDecimal getPrice() {
			return price;
		}

		public void setPrice(BigDecimal price) {
			this.price = price;
		}

	}

	public static class ServiceInfo {
		private String key;
		private String name;
		private String value;
		private boolean menu;
		private int level;
		private Map<String, Object> attrs;

		public ServiceInfo(String key, String name, String value, boolean menu, int level) {
			super();
			this.key = key;
			this.name = name;
			this.value = value;
			this.menu = menu;
			this.level = level;
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public boolean isMenu() {
			return menu;
		}

		public void setMenu(boolean menu) {
			this.menu = menu;
		}

		public int getLevel() {
			return level;
		}

		public void setLevel(int level) {
			this.level = level;
		}

		public Map<String, Object> getAttrs() {
			return attrs;
		}

		public void setAttrs(Map<String, Object> attrs) {
			this.attrs = attrs;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((key == null) ? 0 : key.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ServiceInfo other = (ServiceInfo) obj;
			if (key == null) {
				if (other.key != null)
					return false;
			} else if (!key.equals(other.key))
				return false;
			if (menu) {
				return true;
			} else {
				return false;
			}
		}

	}

}
